<!DOCTYPE html>
<html>
<head>
    <title>Circuit Diagram</title>
</head>
<body>
    <canvas id="circuitCanvas" width="550" height="400" style="border:1px solid #d3d3d3;"></canvas>
    <script>
        const canvas = document.getElementById('circuitCanvas');
        const ctx = canvas.getContext('2d');

        // Style settings
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 2;
        ctx.fillStyle = 'black';
        ctx.font = '20px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        // --- Coordinates ---
        const x_left = 100;
        const x_switch_S = 220;
        const x_comp_center = 340;
        const x_right = 500;

        const y_top = 80;
        const y_switch_X = 160;
        const y_cap_A = 200;
        const y_switch_Y = 260;
        const y_cap_B = 300;
        const y_bottom = 370;

        // --- Drawing Functions ---
        function drawLine(x1, y1, x2, y2) {
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
        }

        function drawBattery(x, y, height, gap) {
            const plate_h = height / 2;
            const short_plate_h = plate_h / 2;
            
            // Positive plate (longer)
            ctx.beginPath();
            ctx.moveTo(x - gap, y - plate_h);
            ctx.lineTo(x - gap, y + plate_h);
            ctx.stroke();
            
            // Negative plate (shorter and thicker)
            const oldWidth = ctx.lineWidth;
            ctx.lineWidth = oldWidth * 2;
            ctx.beginPath();
            ctx.moveTo(x + gap, y - short_plate_h);
            ctx.lineTo(x + gap, y + short_plate_h);
            ctx.stroke();
            ctx.lineWidth = oldWidth;
        }

        function drawCapacitor(x, y, height, gap) {
            const plate_h = height / 2;
            ctx.beginPath();
            ctx.moveTo(x - gap, y - plate_h);
            ctx.lineTo(x - gap, y + plate_h);
            ctx.stroke();
            
            ctx.beginPath();
            ctx.moveTo(x + gap, y - plate_h);
            ctx.lineTo(x + gap, y + plate_h);
            ctx.stroke();
        }

        function drawPoint(x, y, label, label_dx, label_dy) {
            ctx.beginPath();
            ctx.arc(x, y, 4, 0, 2 * Math.PI);
            ctx.fill();
            if (label) {
                ctx.fillText(label, x + label_dx, y + label_dy);
            }
        }
        
        // --- Draw Circuit ---

        // Top circuit loop with battery
        drawLine(x_left, y_top, x_comp_center - 25, y_top);
        drawBattery(x_comp_center, y_top, 40, 10);
        drawLine(x_comp_center + 25, y_top, x_right, y_top);

        // Right vertical wire
        drawLine(x_right, y_top, x_right, y_cap_B);
        drawPoint(x_right, y_cap_A, null);

        // Capacitor A branch
        drawLine(x_right, y_cap_A, x_comp_center + 15, y_cap_A);
        drawCapacitor(x_comp_center, y_cap_A, 40, 15);
        ctx.fillText('A', x_comp_center, y_cap_A - 30);
        drawLine(x_comp_center - 15, y_cap_A, x_switch_S, y_cap_A);

        // Capacitor B branch
        drawLine(x_right, y_cap_B, x_comp_center + 15, y_cap_B);
        drawCapacitor(x_comp_center, y_cap_B, 40, 15);
        ctx.fillText('B', x_comp_center, y_cap_B - 30);
        drawLine(x_comp_center - 15, y_cap_B, x_left, y_cap_B);

        // Left side with switch
        drawLine(x_left, y_top, x_left, y_switch_X);
        
        // Switch S and its connections
        drawPoint(x_left, y_switch_X, 'X', -20, 0); // Point X
        drawPoint(x_switch_S, y_cap_A, 'S', 0, 25); // Point S
        drawLine(x_left, y_switch_X, x_switch_S, y_cap_A); // Switch arm

        // Terminal Y
        drawPoint(x_left, y_switch_Y, 'Y', -20, 0);

        // Figure caption
        ctx.font = 'bold 20px Arial';
        ctx.fillText('Fig. 5.1', canvas.width / 2, y_bottom);
    </script>
</body>
</html>